//
//  BankManagerViewController.m
//  51易借
//
//  Created by 张睿 on 2017/8/28.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "BankManagerViewController.h"
#import "LLPayUtil.h"
#import "LLPaySdk.h"
#import "MemberInfoModel.h"
#import "BankModel.h"
#import "MemberInfoTypePageModel.h"
#import "BankZhiViewController.h"

@interface BankManagerViewController ()<UITextFieldDelegate,LLPaySdkDelegate>
@property (nonatomic,strong)NSArray * bankArr;
@property (nonatomic, retain) NSMutableDictionary *orderDic;

@property (nonatomic, weak)UIView * topView;
@property (nonatomic, weak)UILabel * topLabel;
@property (nonatomic, weak)UIView * inputView;
@property (nonatomic, strong)UILabel * cardholderLabel;
//@property (nonatomic, strong)UITextField * nameLabel;
@property (nonatomic, strong)UILabel * nameLabel;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UILabel * cardNumLabel;
@property (nonatomic, weak)UIButton * issuseButton;
@property (nonatomic, strong)UITextField * cardNumText;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UILabel * bankLabel;
//@property (nonatomic, strong)UITextField * bankNameLabel;
@property (nonatomic, strong)UILabel * bankNameLabel;
@property (nonatomic, weak)UIImageView * rightImageView;
@property (nonatomic, strong)UIView * threeLineView;
@property (nonatomic, strong)UILabel * telNumberLabel;
@property (nonatomic, strong)UITextField * telNumText;
@property (nonatomic, strong)UIView * fourLineView;
@property (nonatomic, strong)UILabel * verifyLabel;
@property (nonatomic, strong)UITextField * verifyText;
@property (nonatomic, weak)UIView * verticalLineView;
@property (nonatomic, weak)UIButton * resentButton;
@property (nonatomic, weak)UIButton * sureButton;
@property (nonatomic,strong)MemberInfoModel * pageModel;
@property (nonatomic,strong)ParaModel * para;
//计时器
@property (nonatomic, strong)NSTimer * countDownTimer;

@property (nonatomic, copy)NSString * bankCode;
@property (nonatomic,assign)BOOL hasInfo;

@property (nonatomic, copy)NSString * acct_name;
@property (nonatomic, copy)NSString * card_no;
@property (nonatomic, copy)NSString * id_no;
@property (nonatomic, copy)NSString * oid_partner;
@property (nonatomic, copy)NSString * sign;
@property (nonatomic, copy)NSString * sign_type;
@property (nonatomic, copy)NSString * user_id;

@end

//倒计时总的秒数
//static NSInteger  secondsCountDown = 60;

@implementation BankManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BACK_COLOR;

    [self setupNavBar];
    [self setupUI];
}


- (void)setupNavBar{

    self.title = @"收款银行卡";

    UIButton * backButton = [[UIButton alloc] init];

    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLastView) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];

    self.navigationItem.leftBarButtonItem = leftButton;
}








- (void)setupUI{

    UIView * topView = [UIView new];

    topView.backgroundColor = TOTAL_COLOR;

    self.topView = topView;

    [self.view addSubview:self.topView];


    UILabel * topLabel = [UILabel new];

    topLabel.textColor = ColorWithRGB(0x7f7f7f);
    topLabel.text = @"请添加您的银行卡用于收款";
    topLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];

    self.topLabel = topLabel;

    [self.view addSubview:self.topLabel];

    UIView * inputView = [UIView new];

    inputView.backgroundColor = [UIColor whiteColor];
    inputView.layer.borderColor = [ColorWithRGB(0xe6e6e6) CGColor];
    inputView.layer.borderWidth = 0.5 * SCREENH6;

    self.inputView = inputView;

    [self.view addSubview:self.inputView];


    self.cardholderLabel = [self setupLaftLabel:@"持卡人"];

    [self.inputView addSubview:self.cardholderLabel];

    
//    self.nameLabel = [self setupTextField:@"请输入持卡人姓名"];
    
    NSString * nameString = DEF_GET_OBJECT(@"name");
    
    NSString * name = [nameString substringFromIndex:1];
    
//    NSLog(@"==%@",name);
//    self.nameLabel.text = [NSString stringWithFormat:@"*%@",name];
    
//    NSLog(@"==%@",self.nameLabel.text);
    
//    self.nameLabel.delegate = self;
    
//    UITextField * textField = [[UITextField alloc] init];
//    
//    textField.borderStyle = UITextBorderStyleNone;
//    textField.backgroundColor = [UIColor clearColor];
//    textField.layer.borderColor = [[UIColor clearColor] CGColor];
//    textField.textAlignment = NSTextAlignmentRight;
//    textField.textColor = ColorWithRGB(0x303c41);
//    textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
//    //设置显示模式为永远显示(默认不显示)
//    textField.leftViewMode = UITextFieldViewModeAlways;
//    //    textField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13 * SCREENH6];
//    textField.font = [UIFont systemFontOfSize:13 * SCREENH6];
//    textField.text = [NSString stringWithFormat:@"*%@",name];
//    textField.delegate = self;
//    
//    
//    
//    
//    self.nameLabel = textField;
//    [self.inputView addSubview:self.nameLabel];
    
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.textColor = ColorWithRGB(0x303c41);
    nameLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    nameLabel.textAlignment = NSTextAlignmentRight;
    nameLabel.text = [NSString stringWithFormat:@"*%@",name];
    
    self.nameLabel = nameLabel;
    
    [self.inputView addSubview:self.nameLabel];
    

    self.oneLineView = [self setupLineView];

    [self.inputView addSubview:self.oneLineView];


    self.cardNumLabel = [self setupLaftLabel:@"卡号"];

    [self.inputView addSubview:self.cardNumLabel];


    UIButton * issuseButton = [UIButton new];

    [issuseButton setImage:[UIImage imageNamed:@"问号"] forState:UIControlStateNormal];
    [issuseButton addTarget:self action:@selector(didWatchBankRules) forControlEvents:UIControlEventTouchUpInside];

    self.issuseButton = issuseButton;

    [self.inputView addSubview:self.issuseButton];


    self.cardNumText = [self setupTextField:@"请输入银行卡号"];
    self.cardNumText.keyboardType = UIKeyboardTypeNumberPad;
    self.cardNumText.delegate = self;

    [self.inputView addSubview:self.cardNumText];

    self.twoLineView = [self setupLineView];

    [self.inputView addSubview:self.twoLineView];

    self.bankLabel = [self setupLaftLabel:@"所属银行"];

    [self.inputView addSubview:self.bankLabel];

//    UILabel * bankNameLabel = [UILabel new];
//
//    bankNameLabel.textColor = ColorWithRGB(0x717171);
//    bankNameLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
//    bankNameLabel.textAlignment = NSTextAlignmentRight;
//    //    bankNameLabel.text = @"建设银行";
//    self.bankNameLabel = [self setupTextField:@"银行名称"];
//    self.bankNameLabel.userInteractionEnabled = NO;
//    [self.inputView addSubview:self.bankNameLabel];


    UILabel * bankNameLabel = [UILabel new];
    
    bankNameLabel.textColor = ColorWithRGB(0x303c41);
    bankNameLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    bankNameLabel.textAlignment = NSTextAlignmentRight;
//    bankNameLabel.text = [NSString stringWithFormat:@"*%@",name];
    
    self.bankNameLabel = bankNameLabel;
    
    [self.inputView addSubview:self.bankNameLabel];

//    UIImageView * rightImageView = [UIImageView new];
//
//    rightImageView.image = [UIImage imageNamed:@"返回-拷贝-6"];
//
//    self.rightImageView = rightImageView;
//
//    [self.inputView addSubview:self.rightImageView];

    self.threeLineView = [self setupLineView];

    [self.inputView addSubview:self.threeLineView];

    self.telNumberLabel = [self setupLaftLabel:@"手机号码"];

    [self.inputView addSubview:self.telNumberLabel];

    self.telNumText = [self setupTextField:@"请输入银行预留手机号"];
    self.telNumText.keyboardType = UIKeyboardTypeNumberPad;

    [self.inputView addSubview:self.telNumText];



    UIButton * sureButton = [UIButton new];
    //300 * 42
    [sureButton setTitle:@"绑定" forState:UIControlStateNormal];
    [sureButton setBackgroundImage:[UIImage imageNamed:@"登录"] forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(didSaveButton) forControlEvents:UIControlEventTouchUpInside];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];

    self.sureButton = sureButton;

    [self.view addSubview:self.sureButton];
    self.cardNumText.delegate = self;
    self.telNumText.delegate = self;
}



- (void)viewDidLayoutSubviews{

    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(64);
    }];

    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topView.mas_bottom).offset(10 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(10 * SCREENH6);
    }];

    [self.inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.topLabel);
        make.top.mas_equalTo(self.topLabel.mas_bottom).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-5 * SCREENH6);
        make.height.mas_equalTo(204 * SCREENH6);
    }];

    [self.cardholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.inputView).offset(8 * SCREENH6);
        make.top.mas_equalTo(self.inputView).offset(18 * SCREENH6);
    }];

    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.cardholderLabel);
        make.trailing.mas_equalTo(self.inputView.mas_trailing).offset(-20 * SCREENH6);
    }];

    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.inputView);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.inputView).offset(50 * SCREENH6);
    }];

    [self.cardNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.cardholderLabel);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(18 * SCREENH6);
    }];

    [self.issuseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(17.5 * SCREENH6);
        make.leading.mas_equalTo(self.cardNumLabel.mas_trailing).offset(2 * SCREENH6);
        make.centerY.mas_equalTo(self.cardNumLabel);
    }];

    [self.cardNumText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.inputView).offset(-20 * SCREENH6);
        make.height.mas_equalTo(40 * SCREENH6);
        make.width.mas_equalTo(250 * SCREENH6);
        make.centerY.mas_equalTo(self.issuseButton);
    }];

    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(50 * SCREENH6);
        make.leading.trailing.height.mas_equalTo(self.oneLineView);
    }];

    [self.telNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.cardNumLabel);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(18 * SCREENH6);
    }];

//    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.bankLabel);
//        make.trailing.mas_equalTo(self.inputView).offset(-10 * SCREENH6);
//        make.width.mas_equalTo(7.5 * SCREENH6);
//        make.height.mas_equalTo(12.5 * SCREENH6);
//    }];

    [self.telNumText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.cardNumText);
        make.centerY.mas_equalTo(self.telNumberLabel);
    }];

    [self.threeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.twoLineView);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(50 * SCREENH6);
    }];

    [self.bankLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.telNumberLabel);
        make.top.mas_equalTo(self.threeLineView.mas_bottom).offset(18 * SCREENH6);
    }];

    [self.bankNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.cardNumText);
        make.centerY.mas_equalTo(self.bankLabel);
    }];

    [self.fourLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.threeLineView);
        make.top.mas_equalTo(self.threeLineView.mas_bottom).offset(50 * SCREENH6);
    }];

    [self.verifyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.telNumberLabel);
        make.top.mas_equalTo(self.fourLineView.mas_bottom).offset(18 * SCREENH6);
    }];

    [self.resentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.inputView.mas_trailing).offset(-10 * SCREENH6);
        make.width.mas_equalTo(90 * SCREENH6);
        make.centerY.mas_equalTo(self.verifyLabel);
    }];

    [self.verticalLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.resentButton.mas_leading).offset(-5 * SCREENH6);
        make.width.mas_equalTo(0.5 * SCREENH6);
        make.top.mas_equalTo(self.fourLineView.mas_bottom).offset(10 * SCREENH6);
        make.bottom.mas_equalTo(self.inputView).offset(-10 * SCREENH6);
    }];

    [self.verifyText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.verifyLabel);
        make.trailing.mas_equalTo(self.verticalLineView.mas_leading).offset(-10 * SCREENH6);
        make.height.mas_equalTo(40 * SCREENH6);
        make.width.mas_equalTo(100 * SCREENH6);
    }];

    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.inputView.mas_bottom).offset(60 * SCREENH6);
        make.width.mas_equalTo(300 * SCREENH6);
        make.height.mas_equalTo(42 * SCREENH6);
    }];
}



- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (textField == self.cardNumText) {
        self.hasInfo = NO;

        if (textField.text.length > 0) {
            
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            
//            ParaModel*para = [ParaModel new];
//            
//            para.cardNo =  textField.text;
            
            NSDictionary * pa = @{@"cardNo":textField.text};
            
//                    NSLog(@"pa==%@",pa);
            
            [[NetworkTool shareManager] POST:@"bank/queryCardBin.json" parameters:pa progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
                
                [MBProgressHUD hideHUDForView:self.view animated:YES];
//                NSLog(@"responseObject==%@",responseObject);
                
                if([[responseObject objectForKey:@"code"]intValue]==0)
                {
                    NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
                    
                    if ([code isEqualToString:@"0"]) {
                        self.hasInfo = YES;
                        NSDictionary * data = responseObject[@"data"];
                        self.bankNameLabel.text = [NSString stringWithFormat:@"%@",data[@"bankName"]];
                        self.bankCode = [NSString stringWithFormat:@"%@",data[@"bankCode"]];
                    }
                }else{
                    
                    [self showHint:[responseObject objectForKey:@"message"]];
                    
                    
                }
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                [self showHint:@"网络错误"];

                
            }];
            
            
//            [[NetworkTool shareManager] POST:[Interfacefile queryCardBin] Param:pa andWaitStr:@"" waitView:self.view success:^(id  _Nullable responseObject) {
//                
//                [MBProgressHUD hideHUDForView:self.view animated:YES];
//                NSLog(@"responseObject==%@",responseObject);
//                
//                if([[responseObject objectForKey:@"code"]intValue]==0)
//                {
//                    NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
//                    
//                    if ([code isEqualToString:@"0"]) {
//                        self.hasInfo = YES;
//                        NSDictionary * data = responseObject[@"data"];
//                        self.bankNameLabel.text = [NSString stringWithFormat:@"%@",data[@"bankName"]];
//                        self.bankCode = [NSString stringWithFormat:@"%@",data[@"bankCode"]];
//                    }
//                }else{
//                    
//                    [self showHint:[responseObject objectForKey:@"message"]];
//                    
//                    
//                }
//            } andFailure:^(NSError * _Nonnull error) {
//                [MBProgressHUD hideHUDForView:self.view animated:YES];
//                [self showHint:@"网络错误"];
//            }];
        }
    }
}


//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//    
//    if (textField == self.nameLabel) {
//        
//       return NO;
//    }else{
//        
//        return YES;
//    }
//    
//}

//- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
//    return NO;
//}


- (void)didSaveButton{
    
    [self.telNumText resignFirstResponder];
    [self.cardNumText resignFirstResponder];
    
    if(self.nameLabel.text.length == 0)
    {
        [self showHint:@"请输入持卡人姓名"];
        return;
    }
    if(self.cardNumText.text.length == 0)
    {
        [self showHint:@"请输入银行卡号"];
        return;
    }
    if(self.hasInfo==NO)
    {
        [self showHint:@"未获取银行卡信息"];
        return;
    }
    BOOL next = [self isMobileNumber:self.telNumText.text];

    if (next) {
        NSDictionary * param = @{@"cardNo":self.cardNumText.text,@"bankCode":self.bankCode,@"cardBindMobile":self.telNumText.text};

        [[NetworkTool shareManager] POST:@"bank/getPreBindCardJson.json" parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {

            NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];

            if ([code isEqualToString:@"0"]) {

                NSDictionary * data = responseObject[@"data"];

                [self payDic:data];


            }


        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

            NSLog(@"==%@",error);

        }];


    }else{


        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请输入正确的手机号"
                                                                preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件

                                                              }];

        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }



    //    [self pay];

}


- (UILabel *)setupLaftLabel:(NSString *)title{

    UILabel * label = [UILabel new];

    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:14 * SCREENH6];
    label.textAlignment = NSTextAlignmentLeft;
    label.text = title;

    return label;
}


- (UITextField *)setupTextField:(NSString *)title{

    UITextField * textField = [[UITextField alloc] init];

    textField.borderStyle = UITextBorderStyleNone;
    textField.backgroundColor = [UIColor clearColor];
    textField.layer.borderColor = [[UIColor clearColor] CGColor];
    textField.textAlignment = NSTextAlignmentRight;
    //设置光标颜色
    textField.tintColor = ColorWithRGB(0xcecece);
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xcecece);
//    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * SCREENH6];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12 * SCREENH6];
    //修改占位符文字颜色
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:title attributes:attrs];
    textField.textColor = ColorWithRGB(0x303c41);
    textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    textField.leftViewMode = UITextFieldViewModeAlways;
//    textField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13 * SCREENH6];
    textField.font = [UIFont systemFontOfSize:13 * SCREENH6];
    

    return textField;
}


- (UIView *)setupLineView{

    UIView * lineView = [UIView new];

    lineView.backgroundColor = BACK_COLOR;

    return lineView;
}


//点击获取验证码按钮
//- (void)didGetVeriftyButton{

//    [self.telNumText resignFirstResponder];
//
//    BOOL next = [self isMobileNumber:self.telNumText.text];
//
//    if (next) {
//        ParaModel*para = [ParaModel new];
//
//
//        [[NetworkTool shareManager] POST:[Interfacefile sendSmsByMobilePhone] Param:[para toDictionary] andWaitStr:@"" waitView:self.view success:^(id  _Nullable responseObject) {
//            if([[responseObject objectForKey:@"code"]intValue]==0)
//            {
//                //        NSLog(@"手机号正确");
//                self.resentButton.enabled = NO;
//                secondsCountDown = 60;
//
//                [self setupDownTimer];
//            }else{
//
//                [self showHint:[responseObject objectForKey:@"message"]];
//
//
//            }
//        } andFailure:^(NSError * _Nonnull error) {
//            [self showHint:@"网络错误"];
//        }];
//
//
//
//    }else{
//
//
//        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
//                                                                       message:@"请输入正确的手机号"
//                                                                preferredStyle:UIAlertControllerStyleAlert];
//
//        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
//                                                              handler:^(UIAlertAction * action) {
//                                                                  //响应事件
//
//                                                              }];
//
//        [alert addAction:defaultAction];
//        [self presentViewController:alert animated:YES completion:nil];
//    }




//}


//- (void)setupDownTimer{
//
//    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
//}
//
//
////定时器的方法
//- (void)countDownAction{
//
//    secondsCountDown --;
//
//    [self.resentButton setTitle:[NSString stringWithFormat:@"%lds后重新获取",(long)secondsCountDown] forState:UIControlStateDisabled];
//
//    //当倒计时到0时做需要的操作，button变成可选状态
//    if(secondsCountDown == 0){
//
//        [_countDownTimer invalidate];
//
//        [self.resentButton setTitle:@"获取验证码" forState:UIControlStateNormal];
//        self.resentButton.enabled = YES;
//    }
//}


//正则表达式判断手机号是否正确
- (BOOL)isMobileNumber:(NSString *)mobileNum {

    //    电信号段:133/149/153/180/181/189/173/177
    //    联通号段:130/131/132/155/156/185/186/145/176
    //    移动号段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
    //    虚拟运营商:170/171

    NSString *MOBILE = @"^1(3[0-9]|4[579]|5[0-35-9]|8[0-9]|7[0136-8])\\d{8}$";

    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];

    return [regextestmobile evaluateWithObject:mobileNum];
}


- (void)didWatchBankRules{
    
    BankZhiViewController * vc = [BankZhiViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];

}

- (void)backToLastView{

    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


-(void)pushBind:(NSDictionary*)dic complete:(void(^)())complete
{



    [[NetworkTool manager] POSTJson:[Interfacefile pushBindCardResult] Param:dic andWaitStr:@"" waitView:self.view success:^(id  _Nullable responseObject) {
        if([[responseObject objectForKey:@"code"]intValue]==0)
        {
            complete();
        }else{

            [self showHint:[responseObject objectForKey:@"message"]];
        }
    } andFailure:^(NSError * _Nonnull error) {
        [self showHint:@"网络错误"];
    }];

}


-(void)getPageModel
{
    ParaModel*para = [ParaModel new];


    [[NetworkTool shareManager] POST:[Interfacefile loadCurrentPersonalInfo] Param:[para toDictionary] andWaitStr:@"" waitView:self.view success:^(id  _Nullable responseObject) {
        if([[responseObject objectForKey:@"code"]intValue]==0)
        {
            self.pageModel = [[MemberInfoModel alloc] initWithDictionary:[responseObject objectForKey:@"data"] error:nil];
            self.para.familyContactType = self.pageModel.familyRelation;
            self.para.familyContactName = self.pageModel.familyContact;
            self.para.familyContactTel = self.pageModel.familyTel;
            self.para.urgencyContactType = self.pageModel.urgencyRelation;
            self.para.urgencyContactName = self.pageModel.urgencyContact;
            self.para.urgencyContactTel = self.pageModel.urgencyTel;
//            [self getTypeModel:nil];

        }else{

            [self showHint:[responseObject objectForKey:@"message"]];


        }
    } andFailure:^(NSError * _Nonnull error) {
        [self showHint:@"网络错误"];
    }];

}

//-(void)getTypeModel:(void(^)())complete
//{
//    ParaModel*para = [ParaModel new];
//
//
//    [[NetworkTool manager] POST:[Interfacefile loadSupportBankList] Param:[para toDictionary] andWaitStr:@"" waitView:nil success:^(id  _Nullable responseObject) {
//        if([[responseObject objectForKey:@"code"]intValue]==0)
//        {
//            self.bankArr = [BankModel arrayOfModelsFromDictionaries:[responseObject objectForKey:@"data"] error:nil];
//            if(complete)
//            {
//                complete();
//            }
//        }else{
//            if(complete)
//                [self showHint:[responseObject objectForKey:@"message"]];
//
//
//        }
//    } andFailure:^(NSError * _Nonnull error) {
//        if(complete)
//            [self showHint:@"网络错误"];
//    }];
//
//}

//-(ParaModel*)para
//{
//    if(!_para)
//    {
//        _para = [ParaModel new];
//    }
//    return _para;
//}
#pragma mark delegate
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
    
    [self.telNumText resignFirstResponder];
    [self.cardNumText resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.view endEditing:YES];
    return YES;
}






- (void)payDic:(NSDictionary*)dic{

    [LLPaySdk sharedSdk].sdkDelegate = self;

    [[LLPaySdk sharedSdk] presentLLPaySignInViewController:self withPayType:LLPayTypeInstalments andTraderInfo:dic];
}



#pragma -mark 支付结果 LLPaySdkDelegate
// 订单支付结果返回，主要是异常和成功的不同状态
// TODO: 开发人员需要根据实际业务调整逻辑
- (void)paymentEnd:(LLPayResult)resultCode withResultDic:(NSDictionary *)dic
{
    NSString *msg = @"支付异常";

    switch (resultCode) {
        case kLLPayResultSuccess:
        {
            msg = @"绑定成功";
//            [self showHint:msg];
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:msg
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];


            [self pushBind:dic complete:^{
                [self.navigationController popViewControllerAnimated:YES];

            }];
        }
            break;
        case kLLPayResultFail:
        {
            msg = @"绑定失败";
            [self showHint:msg];
        }
            break;
        case kLLPayResultCancel:
        {
            msg = @"绑定取消";
            [self showHint:msg];
        }
            break;
        case kLLPayResultInitError:
        {
            msg = @"sdk初始化异常";
            [self showHint:msg];
        }
            break;
        case kLLPayResultInitParamError:
        {
            msg = dic[@"ret_msg"];
            [self showHint:msg];
        }
            break;
        default:
            break;
    }

//    [self showHint:msg];

}


@end

